package com.taofuxn.admin.api.controller.sys;

import com.taofuxn.admin.api.controller.common.BaseController;
import com.taofuxn.admin.api.model.request.SysUserPromiseReq;
import com.taofuxn.admin.api.model.response.SysUserPromiseResp;
import com.taofuxn.admin.api.service.SessionService;
import com.taofuxn.admin.api.service.SysUserConfigService;
import com.taofuxn.admin.common.model.BaseResults;
import com.taofuxn.admin.common.model.PageRequest;
import com.taofuxn.admin.common.model.PageResponse;
import com.taofuxn.admin.dao.entity.TbSysUser;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 用户管理
 */
@Api
@Slf4j
@RequestMapping("/sys/users")
@RestController
public class UsersController extends BaseController {

    @Autowired
    private SysUserConfigService sysUserConfigService;
    @Autowired
    private SessionService sessionService;


    @GetMapping("/list")
    public BaseResults<PageResponse<SysUserPromiseResp>> getUserPage(PageRequest<TbSysUser> pageRequest) {
        PageResponse<SysUserPromiseResp> userModelPageResponse =  sysUserConfigService.getUserPage(pageRequest);
        return BaseResults.success(userModelPageResponse);
    }

    @PostMapping("/edit")
    public BaseResults edit(@RequestBody @Valid @NotNull( message = "修改参数不能为空") SysUserPromiseReq user){
        sysUserConfigService.editUser(user,user.getRoleIds());
        return BaseResults.success(sysUserConfigService.getUserDetailById(user.getId()));
    }

    @GetMapping("/detail/{id}")
    public BaseResults<SysUserPromiseResp> getUserDetail(@PathVariable Long id) {
        SysUserPromiseResp resp =  sysUserConfigService.getUserDetailById(id);
        return BaseResults.success(resp);
    }




}
